package com.byinsight.web.actions.dimension;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.byinsight.service.dto.Dimension;
import com.byinsight.web.actions.CheckSessionAction;
import com.byinsight.web.actions.Constants;
import com.byinsight.web.actions.util.DimensionHelper;
import com.byinsight.web.actions.util.ExpenseHelper;
import com.byinsight.web.actions.util.TimeDimensionDefinition;

public class GetDimensions4ExpenseInput extends CheckSessionAction {
	private static final long serialVersionUID = -2029942778954525528L;
	private static final Logger logger = Logger.getLogger(GetDimensions4ExpenseInput.class.getName());
	
	private class ColInfo {		
		public static final String TYPE_MEASURE = "measure";
		public static final String TYPE_DIMENSION = "dimension";
		public static final String TYPE_DATE = "date";
		public static final String TYPE_REMARK = "remark";
		public static final String TYPE_TEXT = "text";	
		public static final String TYPE_TAG = "tag";
		
		public String type;
		public String displayName;
		public String dimdef;
		public String id;
		
		public ColInfo(String type, String displayName, String dimdef, String id) {
			this.type = type;
			this.displayName = displayName;
			this.dimdef = dimdef;
			this.id = id;
		}
		
		public String toXMLString() {
			StringBuffer sb = new StringBuffer();
			sb.append("<col>");
			sb.append("<type>").append(type).append("</type>");
			sb.append("<displayName>").append(displayName).append("</displayName>");
			sb.append("<id>").append(id).append("</id>");
			if (dimdef != null) {
				sb.append("<dimdef>").append(dimdef).append("</dimdef>");
			}
			sb.append("</col>");
			return sb.toString();
		}
	}
	
	@Override
	protected String process() {		
		List<ColInfo> cols = new ArrayList<ColInfo>();
		cols.addAll(getStaticColumns());
		cols.addAll(getDimensionColumns());
		cols.add(getMerchantColumn());
		cols.add(getTagColumn());
		cols.add(getRemarkColumn());
		
		StringBuffer sb = new StringBuffer();
		sb.append("<colInfo>");
		for (ColInfo col : cols) {
			sb.append(col.toXMLString());
		}
		sb.append("</colInfo>");
		return sb.toString();
	}
	
	private List<ColInfo> getStaticColumns() {
		List<ColInfo> ret = new ArrayList<ColInfo>();
		ret.add(new ColInfo(ColInfo.TYPE_DATE, "日期", TimeDimensionDefinition.getXml(), Constants.FIELD_EXP_DATE));
		ret.add(new ColInfo(ColInfo.TYPE_TEXT, "说明", null, Constants.FIELD_EXP_DESC));
		ret.add(new ColInfo(ColInfo.TYPE_MEASURE, "金额", null, Constants.FIELD_EXP_AMOUNT));
		return ret;
	}
	
	private List<ColInfo> getDimensionColumns() {
		logger.warning("begin get dimension columns, dimensionService is null: " + (dimensionService == null) + ".");
		List<Dimension> dimensions = dimensionService.getDimensions4Input();
		logger.warning("dim size: " + dimensions.size());
		List<ColInfo> ret = new ArrayList<ColInfo>();
		for (Dimension dim : dimensions) {
			String dimdef = DimensionHelper.toDimDefXml(dim).toString();
			logger.warning("dim def: " + dimdef);
			ret.add(new ColInfo("dimension", dim.getName(), dimdef, ExpenseHelper.getDimensionTag(dim)));			
		}
		return ret;
	}
	
	private ColInfo getRemarkColumn() {
		return new ColInfo(ColInfo.TYPE_REMARK, "备注", null, Constants.FIELD_EXP_REMARK);
	}
	
	private ColInfo getMerchantColumn() {
		List<String> merchants = expenseManager.findShopNames("");
		return new ColInfo(ColInfo.TYPE_TAG, "商家", DimensionHelper.tagsToXml(merchants), Constants.FIELD_EXP_MERCHANT);
	}
	
	private ColInfo getTagColumn() {
		List<String> tags = expenseManager.findTagNames("");
		return new ColInfo(ColInfo.TYPE_TAG, "标签", DimensionHelper.tagsToXml(tags), Constants.FIELD_EXP_TAG);
	}

}
